﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.ProgressBar;
using YArchitech.Revit;
using YJKCodeBase.WinForm;
using YJKOpeningCommon.OpeningEX;

namespace YJKOpeningCreate
{
	public class OpeningData : IComparable<OpeningData>
	{
		private void OpeningDataIni()
		{
			if (this.m_ListOpening3D.Count > 0)
			{
				Element element = this.m_ListOpening3D[0];
				OpeningXData openingXData = new OpeningXData();
				if (element == null || openingXData == null)
				{
					return;
				}
				openingXData.ReadOpeningXData(element);
				this.m_strCreateData = openingXData.m_strCreateOpeningDate;
				this.m_strSpeciality = openingXData.m_strSpeciality;
				this.m_strUserName = openingXData.m_strDesignUser;
			}
		}

		public void OpeningDataIni(Element opening)
		{
			OpeningXData openingXData = new OpeningXData();
			if (opening == null || openingXData == null)
			{
				return;
			}
			openingXData.ReadOpeningXData(opening);
			this.m_strCreateData = openingXData.m_strCreateOpeningDate;
			this.m_strSpeciality = openingXData.m_strSpeciality;
			this.m_strUserName = openingXData.m_strDesignUser;
			this.m_ListOpening3D.Add(opening);
		}

		public override bool Equals(object obj)
		{
			this.OpeningDataIni();
			Element element = obj as Element;
			OpeningXData openingXData = new OpeningXData();
			if (element == null || openingXData == null)
			{
				return false;
			}
			openingXData.ReadOpeningXData(element);
			return openingXData.m_strCreateOpeningDate.CompareTo(this.m_strCreateData) == 0 && openingXData.m_strDesignUser.CompareTo(this.m_strUserName) == 0 && openingXData.m_strSpeciality.CompareTo(this.m_strSpeciality) == 0;
		}

		private List<Element> GetOpening2D(Element ele3D)
		{
			List<Element> list = new List<Element>();
			OpeningXData openingXData = new OpeningXData();
			if (ele3D == null || openingXData == null)
			{
				return null;
			}
			openingXData.ReadOpeningXData(ele3D);
			string str2DUniqueId = openingXData.m_str2DUniqueId;
			if (str2DUniqueId != null)
			{
				Element element = this.m_doc.GetElement(str2DUniqueId);
				if (element != null)
				{
					list.Add(element);
				}
			}
			List<string> lstLineGuid = openingXData.m_lstLineGuid;
			if (lstLineGuid != null)
			{
				foreach (string text in lstLineGuid)
				{
					Element element2 = this.m_doc.GetElement(text);
					if (element2 != null)
					{
						list.Add(element2);
					}
				}
			}
			return list;
		}

		public bool RemoveOpenings(WindowHandle m_HWndRevit)
		{
			ProgressBarForm progressBarForm = new ProgressBarForm(this.m_ListOpening3D.Count, ProgressBarStyle.Blocks, true);
			progressBarForm.Text = "正在创建预览...";
			progressBarForm.ShowInTaskbar = false;
			progressBarForm.Show(m_HWndRevit);
			Transaction transaction = new Transaction(this.m_doc);
			try
			{
				transaction.Start("delOpenings");
				foreach (Element element in this.m_ListOpening3D)
				{
					List<Element> opening2D = this.GetOpening2D(element);
					foreach (Element elem in opening2D)
					{
						this.m_doc.DeleteElement(elem);
					}
					this.m_doc.DeleteElement(element);
					progressBarForm.RefreshProgressBar();
					if (progressBarForm.m_stopTag)
					{
						return false;
					}
				}
				transaction.Commit();
				progressBarForm.Dispose();
			}
			catch
			{
				transaction.RollBack();
				progressBarForm.Dispose();
				return false;
			}
			return true;
		}

		public int CompareTo(OpeningData other)
		{
			DateTime dateTime = Convert.ToDateTime(this.m_strCreateData);
			if (other == null)
			{
				return -1;
			}
			DateTime value = Convert.ToDateTime(other.m_strCreateData);
			if (dateTime.CompareTo(value) <= 0)
			{
				return 1;
			}
			return -1;
		}

		public Document m_doc;

		public List<Element> m_ListOpening3D = new List<Element>();

		public string m_strSpeciality;

		public string m_strUserName;

		public string m_strCreateData;
	}
}
